package com.dzl.cloud.controller;



import com.dzl.cloud.dto.LoginDTO;
import com.dzl.cloud.dto.ResetPasswordDTO;
import com.dzl.cloud.dto.UpdateUserDTO;
import com.dzl.cloud.entity.User;
import com.dzl.cloud.service.UserService;
import com.dzl.cloud.util.JwtUtils;
import com.dzl.cloud.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.http.MethodNotSupportedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author 光的代言人
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 注册
     */
    @PostMapping("/user/register")
    public Result register(@RequestBody User user, HttpServletRequest request) {
        userService.registerUser(user, request);
        return Result.success();
    }

    /**
     * 登录
     */
    @PostMapping("/user/login")
    public Result login(@RequestBody LoginDTO LoginRequest,HttpServletRequest request) {
        User user = userService.login(LoginRequest,request);
        // 生成 JWT
        String token = JwtUtils.generateToken(user.getUserId(), user.getUsername());
        return Result.success(Collections.singletonMap("token", token));
    }

    /**
     * 获取用户列表
     */
    @GetMapping("/users")
    public Map<String, Object> getUsers(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer limit, @RequestHeader(value = "Authorization") String token) {
        PageHelper.startPage(pageNum, limit);
        Long currentUserId = JwtUtils.getUserIdFromToken(token);
        List<User> users = userService.getUserListWithPermission(currentUserId);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        Map<String, Object> result = new HashMap<>();
        result.put("total", pageInfo.getTotal());
        result.put("users", pageInfo.getList());
        return result;
    }

    /**
     * 获取用户详情
     */
    @GetMapping("/user/{userId}")
    public Result getUserInfo(@PathVariable("userId") Long userId, @RequestHeader("Authorization") String token) {
        Long currentUserId = JwtUtils.getUserIdFromToken(token);
        User user = userService.findById(userId, currentUserId);
        return Result.success(user);
    }

    /**
     * 修改用户信息
     */
    @PutMapping("/user/{userId}")
    public Result updateUser(@PathVariable Long userId, @RequestBody UpdateUserDTO request, @RequestHeader("Authorization") String token,HttpServletRequest request2) {
        Long currentUserId = JwtUtils.getUserIdFromToken(token);
        userService.updateUserWithPermission(userId, request, currentUserId, request2);
        return Result.success("用户信息修改成功！");
    }

    /**
     * 密码重置
     */
    @PostMapping("/user/reset-password")
    public Result resetPassword(@RequestBody ResetPasswordDTO request, @RequestHeader("Authorization") String token) {
        Long currentUserId = JwtUtils.getUserIdFromToken(token);
        userService.resetPassword(request.getUserId(), request.getNewPassword(), currentUserId);
        return Result.success("密码重置成功！");
    }
}
